home *** CD-ROM | disk | FTP | other *** search
/ Freelog 117 / FreelogNo117-OctobreNovembre2013.iso / Programmation / jedit / jedit5.1.0install.exe / {app} / macros / Editing / Greedy_Backspace.bsh < prev    next >
Text File  |  2013-07-28  |  2KB  |  82 lines

  1. /*
  2.  * Greedy_Backspace.bsh - If buffer is using soft tabs,
  3.  * this macro will backspace to the previous tab stop,
  4.  * if all characters between the caret and the tab stop
  5.  * are spaces.  In all other cases a single character is
  6.  * removed.
  7.  *
  8.  * Copyright (C) 2002-2004 Ollie Rutherfurd <oliver@jedit.org>
  9.  *
  10.  * $Id: Greedy_Backspace.bsh 5230 2005-07-20 13:31:08Z orutherfurd $
  11.  */
  12.  
  13. /**
  14.  * @param onlyFullTabs if true, multiple spaces are only
  15.  *                     removed if they would constitute
  16.  *                     a 'complete' tab.
  17.  */
  18. void greedyBackspace(View view, boolean onlyFullTabs)
  19. {
  20.     JEditTextArea textArea = view.getTextArea();
  21.     JEditBuffer buffer = textArea.getBuffer();
  22.  
  23.     int caret = textArea.getCaretPosition();
  24.     int caretLine = textArea.getCaretLine();
  25.     int lineStart = textArea.getLineStartOffset(caretLine);
  26.  
  27.     if(buffer.getBooleanProperty("noTabs") == true)
  28.     {
  29.         // if anything is selected, use standard 
  30.         if(textArea.getSelection().length != 0)
  31.         {
  32.             textArea.backspace();
  33.         }
  34.         // if at the start of the line, use standard
  35.         else if(caret == lineStart)
  36.         {
  37.             textArea.backspace();
  38.         }
  39.         else
  40.         {
  41.             int col = caret - lineStart;
  42.             int tabSize = buffer.getIntegerProperty("tabSize",8);
  43.  
  44.             // unlikely, but just in case
  45.             if(tabSize <= 0)
  46.             {
  47.                 buffer.remove(caret-1,1);
  48.             }
  49.             else
  50.             {
  51.                 int toTabStop = ((col-1) % tabSize) + 1;
  52.                 int count = 0;
  53.                 String chunk = buffer.getText(caret-toTabStop,toTabStop);
  54.                 for(int i=0; i < toTabStop; i++)
  55.                 {
  56.                     if(' ' != chunk.charAt(i))
  57.                         break;
  58.                     count += 1;
  59.                 }
  60.  
  61.                 // if onlyFullTabs must be only spaces to
  62.                 // the tabStop and must have tabSize number 
  63.                 // of spaces to remove them all.
  64.                 if(onlyFullTabs == false || count == tabSize){
  65.                     buffer.remove(caret-count,count);
  66.                 }
  67.                 else{
  68.                     buffer.remove(caret-1,1);
  69.                 }
  70.             }
  71.         }
  72.     }
  73.     else
  74.         textArea.backspace();
  75. }
  76.  
  77. if(buffer.isReadOnly())
  78.     Toolkit.getDefaultToolkit().beep();
  79. else
  80.     greedyBackspace(view,true);
  81.  
  82.